﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace WFBSYS.Model
{
    class DBEntity
    {
        private OleDbConnection connection;
        private OleDbDataReader aReader;

        public int sqlWrite(String sql, OleDbParameter[] parameters) {
            if (this.openConnection())
            {
                OleDbCommand cmd = new OleDbCommand(sql, this.connection);
                if (parameters.Length > 0)
                {
                    for (int i = 0; i < parameters.Length; i++) {
                        cmd.Parameters.Add(parameters[i]);
                    }
                }
                int ret = cmd.ExecuteNonQuery();
                int retId = 0;
                if (ret > 0) {
                    OleDbCommand cmd1 = new OleDbCommand("select @@Identity as Id", this.connection);
                    retId = (Int32)cmd1.ExecuteScalar();
                }
                this.closeConnection();
                return retId;
            }
            return -1;
        }

        public OleDbDataReader getDataReader(String sql)
        {
            if (this.openConnection())
            {
                OleDbCommand aCommand = new OleDbCommand(sql, connection);
                this.aReader = aCommand.ExecuteReader();
                return this.aReader;
            }
            return null;
        }

        public void closeDataReader()
        {
            try
            {
                this.aReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            this.closeConnection();
        }

        /**
         * open
         */
        private bool openConnection()
        {
            String connectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = D:\\Workspace\\TianshengSystem\\DB\\wfbsys.accdb;";
            this.connection = new OleDbConnection(connectionString);
            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        /**
         * close
         */
        private bool closeConnection()
        {
            try
            {
                this.connection.Close();
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }

        public void test()
        {
            OleDbConnection connection = new OleDbConnection("");
            OleDbCommand cmd = new OleDbCommand();
            OleDbTransaction transaction = null;
            cmd.Connection = connection;
            connection.Open();
            transaction = connection.BeginTransaction();
            cmd.Transaction = transaction;
            cmd.CommandText = "";
            cmd.ExecuteNonQuery();
            cmd.CommandText = "";
            cmd.ExecuteNonQuery();
            transaction.Commit();
        }
    }
}


